# Contributor: Jeongbin Park <pjb7687@gmail.com>

_realname=eccodes
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.19.1
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
pkgdesc="ECMWF's GRIB and BUFR encoding/decoding library (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-libaec"
         "${MINGW_PACKAGE_PREFIX}-libpng")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "make"
             "patch"
             "${MINGW_PACKAGE_PREFIX}-cmake")
license=('Apache')
url="https://confluence.ecmwf.int/display/ECC/ecCodes+Home"
source=(https://confluence.ecmwf.int/download/attachments/45757960/${_realname}-${pkgver}-Source.tar.gz
        fix-mingw.patch)
noextract=(${_realname}-${pkgver}-Source.tar.gz)
sha256sums=('9964bed5058e873d514bd4920951122a95963128b12f55aa199d9afbafdd5d4b'
            'ee7d576af56ae23c954b1ee25562475b6db6e04612c39d7f7a16e8a16eb56c33')

prepare() {
  tar xf ${srcdir}/${_realname}-${pkgver}-Source.tar.gz || \
  tar xf ${srcdir}/${_realname}-${pkgver}-Source.tar.gz
  cd ${srcdir}/${_realname}-${pkgver}-Source
  patch -p1 -i ${srcdir}/fix-mingw.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  # CMake options copied from:
  #   https://github.com/ecmwf/eccodes/blob/master/.appveyor.yml

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DENABLE_FORTRAN=0 \
    -DENABLE_PYTHON=0 \
    -DENABLE_NETCDF=1 \
    -DENABLE_JPG=1 \
    -DIEEE_LE=1 \
    -DENABLE_MEMFS=0 \
    -DENABLE_EXAMPLES=OFF \
    -DENABLE_TESTS=OFF \
    ../${_realname}-${pkgver}-Source

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/licenses/$_realname
  cp -pf ${srcdir}/${_realname}-${pkgver}-Source/NOTICE \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}
  cp -pf ${srcdir}/${_realname}-${pkgver}-Source/LICENSE \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}
}
